package com.tnovoselec.android.travelassistant.overlay;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;
import com.tnovoselec.android.travelassistant.Constant;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;
import com.tnovoselec.android.travelassistant.view.CustomMapView;

public class BenzinskeOverlay extends AbstractMapOverlay<IGeoPoint> {

	public interface BenzinskaSelectedListener {
		public void onBenzinskaSelected();
	}

	private Drawable inaMarker;
	private Drawable omvMarker;
	private Drawable petrolMarker;
	private Drawable lpgMarker;
	private Drawable crobenzMarker;

	private BenzinskaSelectedListener listener;

	public BenzinskeOverlay(Context context, Drawable defaultMarker, List<IGeoPoint> items, CustomMapView mapview) {
		super(boundCenter(defaultMarker));
		this.mContext = context;

		inaMarker = context.getResources().getDrawable(R.drawable.ina_icon);
		omvMarker = context.getResources().getDrawable(R.drawable.omv_icon);
		lpgMarker = context.getResources().getDrawable(R.drawable.lpg_icon);
		petrolMarker = context.getResources().getDrawable(R.drawable.petrol_icon);
		crobenzMarker = context.getResources().getDrawable(R.drawable.crobenz_icon);

		this.items = items;
		overlayItems = new ArrayList<OverlayItem>();
		if (items != null) {
			for (IGeoPoint benzinska : items) {
				BenzinskaOverlayItem item = new BenzinskaOverlayItem(
						new GeoPoint((int) (benzinska.getLatitude() * 1e6), (int) (benzinska.getLongitude() * 1e6)), benzinska.getGrad() + " "
								+ benzinska.getAdresa(), benzinska.getBank_id(), benzinska.getBank_id());
				item.setMarker(defaultMarker);
				overlayItems.add(item);
			}
		}

		popupView = LayoutInflater.from(context).inflate(R.layout.map_popup, null);
		popupView.setOnClickListener(this);
		this.mapview = mapview;
		populate();
	}

	@Override
	protected boolean onTap(int index) {
		super.onTap(index);

		if (listener != null) {
			listener.onBenzinskaSelected();
		}
		return true;
	}

	private class BenzinskaOverlayItem extends OverlayItem {

		private String type;

		public BenzinskaOverlayItem(GeoPoint point, String title, String snippet, String type) {
			super(point, title, snippet);
			this.type = type;
		}

		@Override
		public Drawable getMarker(int stateBitset) {
			Drawable drawable = super.getMarker(stateBitset);
			if (type.toLowerCase().contains(Constant.INA_REGEX)) {
				drawable = inaMarker;
			} else if (type.toLowerCase().contains(Constant.CROBENZ_REGEX)) {
				drawable = crobenzMarker;
			} else if (type.toLowerCase().contains(Constant.PETROL_REGEX)) {
				drawable = petrolMarker;
			} else if (type.toLowerCase().contains(Constant.OMV_REGEX)) {
				drawable = omvMarker;
			} else if (type.toLowerCase().contains(Constant.LPG_REGEX)) {
				drawable = lpgMarker;
			}
			return boundCenter(drawable);
		}

	}

	public void setListener(BenzinskaSelectedListener listener) {
		this.listener = listener;
	}

}
